import Vue from 'vue'
import Vuex from 'vuex'
import state from './state'
import mutations from './mutations'
import { TCookies, api } from '@/utils'
Vue.use(Vuex)
let index = 1
const store = () => new Vuex.Store({
  strict: process.env.NODE_ENV !== 'production',
  state,
  actions: {
    /* 退出重置vuex */
    signOut ({ commit }) {
      commit('SET_USER_INFO', {})
      commit('SET_AUTH_INFO', {})
      commit('SET_HOLLAND_TEST', {})
      commit('SET_HOLLAND_LIST', {})
    },
    async getUserInfo ({ commit }, app) {
      try {
        const data = await app.$axios.get(api.getUserInfo)
        console.log(data)
        commit('SET_USER_INFO', data)
      } catch (err) {
        commit('SET_USER_INFO', {})
      }
    },
    async nuxtServerInit ({ commit, dispatch }, { req, app, route }) {
      if (route.name) {
        console.log(index++)
        const { authInfo, hollandTest } = TCookies(req)
        commit('SET_AUTH_INFO', authInfo ? authInfo : {})
        commit('SET_HOLLAND_TEST', hollandTest ? hollandTest : {})
        console.log(route.name)
        /* 获取用户信息 */
        if (authInfo && authInfo.token) {
          await dispatch('getUserInfo', app)
        } else {
          commit('SET_USER_INFO', {})
        }
      }
    }
  },
  mutations
})
export default store
